package com.example.presentshopping.ui.fragment.earnings

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.text.Html
import android.view.View
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseFragment
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.MyearningsBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.utils.tool.GsonUtils
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import kotlinx.android.synthetic.main.activity_myearnings.*

class TodatearninFragment(override var context: Context, var str: String) :BaseMvpFragment() {


    var  myeaemin: MyearningsBean?=null
    var baseRecyclerAdapter: BaseRecyclerAdapter<MyearningsBean.DataBean.ListBean>? = null
    var page =1
    var  recycler_view : SwipeRecyclerView?=null
    var  count_total: TextView ?=null
    private var sendMesPresenter: PublicPresenter? = null


    override fun initView(view: View?, savedInstanceState: Bundle?) {
        sendMesPresenter = PublicPresenter(context,false,"")
        sendMesPresenter?.attachView(this)
        sendMesPresenter?.setHaveCode(false)
        recycler_view =view?.findViewById(R.id.recycler_view)
        count_total =view?.findViewById(R.id.count_total)
        setAdap()
        OnsInit()
        recycler_view?.useDefaultLoadMore()
        recycler_view?.loadMoreFinish(false, true)
        recycler_view?.setLoadMoreListener {
            page++
            OnsInit()
        }

    }


    fun setAdap(){
        baseRecyclerAdapter = object :BaseRecyclerAdapter<MyearningsBean.DataBean.ListBean>(context,null,R.layout.item_myearnings){
            @SuppressLint("SetTextI18n")
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: MyearningsBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val from_type = holder?.getView<TextView>(R.id.from_type)
                val type = holder?.getView<TextView>(R.id.type)
                val total = holder?.getView<TextView>(R.id.total)
                val order_id = holder?.getView<TextView>(R.id.order_id)

                total?.text ="+ "+data.total
                from_type?.text = when(data.from_type){
                    1->{ "红包" }
                    2->{"分润"}
                    3->{"团队收益"}
                    4->{"退款"}
                    else -> ""
                }

                type?.text =data.create_time
                order_id?.text = "订单编号: "+data.order_id

            }

        }
        recycler_view?.adapter =baseRecyclerAdapter
    }


    fun OnsInit(){
        sendMesPresenter?.netWorkRequestPost(
            HttpURL.Req_getRedpaper,
            HttpRequest.getTodatearnin(str,page,10))
    }


    override fun onSuccess(data: String?) {
        myeaemin = GsonUtils.getGson().fromJson(data,MyearningsBean().javaClass)
        recycler_view?.loadMoreFinish(false, true)
        baseRecyclerAdapter?.setData(myeaemin?.data?.list)
        var textsoo =""
        when(str){
            "1"->{
                count_total?.text =    Html.fromHtml("昨日收益: "+"<font color=\"#ff2555\">"+myeaemin?.data?.today_total+"</font>"+"元 \n \n 总收益: "+"<font color=\"#ff2555\">"+myeaemin?.data?.count_total+"</font>"+"元")

            }
          "2"->{
              count_total?.text =    Html.fromHtml("今日收益: "+"<font color=\"#ff2555\">"+myeaemin?.data?.today_total+"</font>"+"元  \n \n 总收益: "+"<font color=\"#ff2555\">"+myeaemin?.data?.count_total+"</font>"+"元")

          }
        }

    }

    override fun onDestroyView() {
        sendMesPresenter?.detachView()
        super.onDestroyView()
    }
    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    override val layoutId: Int
        get() = R.layout.todatearnin_fragment
}